package com.qifan.energy.fragment;


import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.qifan.energy.activity.CouponActivity;
import com.qifan.energy.activity.MessageActivity;
import com.qifan.energy.activity.QuickList2Activity;
import com.qifan.energy.activity.QuickListActivity;
import com.qifan.energy.base.BaseFragment;
import com.qifan.energy.bean.entity.HomeProductMultiItemEntity;
import com.qifan.energy.bean.result.BaseResult;
import com.qifan.energy.mvp.callback.IEventCallback;
import com.qifan.energy.R;
import com.qifan.energy.activity.GoodsDetailActivity;
import com.qifan.energy.activity.GoodsListActivity;
import com.qifan.energy.activity.SearchActivity;
import com.qifan.energy.adapter.HomeListAdapter;
import com.qifan.energy.adapter.ImageNetAdapter;
import com.qifan.energy.bean.result.AppVersionResult;
import com.qifan.energy.bean.result.HomeListResult;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.mvp.callback.IOnClickIndexListener;
import com.qifan.energy.util.CheckUtil;
import com.qifan.energy.util.IntentUtils;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.util.UIUtils;
import com.qifan.energy.widget.CustomBannerView;
import com.qifan.energy.widget.CustomQuickView;
import com.qifan.energy.widget.dialog.UpdateNoticeDialog;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.transformer.AlphaPageTransformer;
import com.youth.banner.transformer.MZScaleInTransformer;
import com.youth.banner.transformer.ScaleInTransformer;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class HomeFragment extends BaseFragment {

    /* 头部banner */
    private CustomBannerView bannerView;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private HomeListAdapter adapter;
    private List<HomeListResult.AdvertiseListBean> images = new ArrayList();
    List<HomeProductMultiItemEntity> dataList = new ArrayList<>();

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_fragment;
    }

    @Override
    protected void initView(View view) {
        adapter = new HomeListAdapter(dataList);
        /* 监听item点击 */
        adapter.setOnItemClickListener((a,v,p) -> {
            HomeProductMultiItemEntity itemEntity  = (HomeProductMultiItemEntity)a.getData().get(p);
            if (!itemEntity.isHeader) {
                Intent intent = new Intent();
                intent.putExtra(Keys.DETAIL_ID_KEY,itemEntity.getResult().getId());
                IntentUtils.startActivity(getActivity(), GoodsDetailActivity.class,intent);
            }
        });
        /* 监听子控件点击 */
        adapter.setOnItemChildClickListener(this::onItemChildClick);
        final GridLayoutManager layoutManager = new GridLayoutManager(getActivity(),2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(getActivity())
                .sizeResId(R.dimen.dp_1)
                .colorResId(R.color.divider_color)
                .build());
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .sizeResId(R.dimen.dp_1)
                .colorResId(R.color.divider_color)
                .build());
        //recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        //设置banner
        setUpHeaderView();
    }

    /* 设置banner */
    private void setUpHeaderView(){
        /* 添加头部视图 */
        bannerView = new CustomBannerView(getActivity());
        adapter.addHeaderView(bannerView);

        //快捷专区
        CustomQuickView quickView = new CustomQuickView(getActivity());
        quickView.setOnClickIndexListener((index)->{
            if (index>20){
                IntentUtils.startActivity(getActivity(), QuickListActivity.class);
            } else {
                IntentUtils.startActivity(getActivity(), QuickList2Activity.class);
            }
        });
        adapter.addHeaderView(quickView);
    }

    @Override
    public void initData() {
        // 网络请求
        homeContentData();
        //版本控制接口
        /*appVersionControl();*/
    }

    /* 版本控制 */
    private void appVersionControl(){
        NetUtils.appVersionControl(new MyObserver<AppVersionResult>(getActivity()) {
            @Override
            public void onSuccess(AppVersionResult appVersionResult) {
                Logger.i("点击确定");
                if (appVersionResult != null) {
                    if (appVersionResult.getIsShowNotice() == 1 && !TextUtils.isEmpty(appVersionResult.getNotice())) {//显示公告
                        final int times = SharedPreferencesUtil.getData(Keys.UPDATE_VERSION_KEY,0);
                        SharedPreferencesUtil.putData(Keys.UPDATE_VERSION_KEY,times + 1);
                        if (times%2 == 0) {
                            new UpdateNoticeDialog().showNow2(getParentFragmentManager(),"update_notice")
                                    .setContent(appVersionResult.getNotice())
                                    .setEventCallback(new IEventCallback() {
                                        @Override
                                        public void onCancel() {
                                            //ToastUtils.showShort("取消");
                                        }

                                        @Override
                                        public void onComfirm() {
                                            //ToastUtils.showShort("确定" + times);
                                            SharedPreferencesUtil.putData(Keys.UPDATE_VERSION_KEY,1);
                                        }
                                    });
                        }
                    }
                }
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                Logger.i(errorMsg);
                //ToastUtils.showShort(errorMsg);
            }
        });
    }

    /* 首页接口列表数据 */
    private void homeContentData(){
        NetUtils.homeContent(new MyObserver<HomeListResult>(getActivity()) {
            @Override
            public void onSuccess(HomeListResult result) {

                /* 为banner赋值 */
                images = result.getAdvertiseList();
                bannerView.setImages(images);

                /* 先清空数据 */
                dataList.clear();
                /* 分销商品区列表 */
                if (CheckUtil.hasContent(result.getDistributePmsProductList())) {
                    dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.DISTRIBUTE,true, "今日特卖"));
                    for (HomeListResult.DistributePmsProductListBean bean: result.getDistributePmsProductList()) {
                        dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.DISTRIBUTE, bean));
                    }
                }

                /* 热门推荐 */
                if (CheckUtil.hasContent(result.getDistributePmsProductList())) {
                    dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL,true, "热门推荐"));
                    for (HomeListResult.DistributePmsProductListBean bean: result.getDistributePmsProductList()) {
                        dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL, bean));
                    }
                }

                /* 母婴精选 */
                if (CheckUtil.hasContent(result.getDistributePmsProductList())) {
                    dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL,true, "母婴精选"));
                    for (HomeListResult.DistributePmsProductListBean bean: result.getDistributePmsProductList()) {
                        dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL, bean));
                    }
                }

                /* 美妆精选 */
                if (CheckUtil.hasContent(result.getDistributePmsProductList())) {
                    dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL,true, "美妆精选"));
                    for (HomeListResult.DistributePmsProductListBean bean: result.getDistributePmsProductList()) {
                        dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL, bean));
                    }
                }

                /* 猜你喜欢 */
                if (CheckUtil.hasContent(result.getDistributePmsProductList())) {
                    dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL,true, "猜你喜欢"));
                    for (HomeListResult.DistributePmsProductListBean bean: result.getDistributePmsProductList()) {
                        dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL, bean));
                    }
                }

                /*if (CheckUtil.hasContent(result.getNormalPmsProductList())) {
                    dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL,true, "妈咪专区"));
                    for (HomeListResult.NormalPmsProductListBean bean: result.getNormalPmsProductList()) {
                        dataList.add(new HomeProductMultiItemEntity(HomeProductMultiItemEntity.NORMAL, bean));
                    }
                }*/

                //刷新
                adapter.setNewData(dataList);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                //提示
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick(R.id.search_ll)
    void onSearchLlClicked(){
        IntentUtils.startActivity(getActivity(), SearchActivity.class);
    }

    @OnClick({R.id.top_message_tv})
    void onTopMessageTvClicked(){
        IntentUtils.startActivity(getActivity(), MessageActivity.class);
    }

    private void onItemChildClick(BaseQuickAdapter a, View v, int p) {
        switch (v.getId()) {
            case R.id.more_ll:
                HomeProductMultiItemEntity itemEntity = (HomeProductMultiItemEntity) a.getData().get(p);
                Intent intent = new Intent();
                intent.putExtra(Keys.HOME_SHOW_TYPE, itemEntity.getItemType());
                IntentUtils.startActivity(getActivity(), GoodsListActivity.class, intent);
                switch (itemEntity.getItemType()) {
                    case HomeProductMultiItemEntity.DISTRIBUTE:
                        break;
                    case HomeProductMultiItemEntity.NORMAL:
                        break;
                    default:
                        break;
                }
                break;
            default:
                break;
        }
    }
}
